package th.kmutt.se.is.fact.adapter;

import java.text.DecimalFormat;
import th.kmutt.se.is.R;
import th.kmutt.se.is.model.Charges;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ChargesAdapter extends ArrayAdapter<Charges> {

	Context context;
	int layoutResourceId;
	Charges data[] = null;

	public ChargesAdapter(Context context, int layoutResourceId, Charges[] data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		Holder holder = null;
		String suffix;
		DecimalFormat df = new DecimalFormat("#,##0.00");

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new Holder();
			holder.chargesName = (TextView) row.findViewById(R.id.chargeName);
			holder.chargesPrice = (TextView) row.findViewById(R.id.chargesPrice);

			row.setTag(holder);
		} else {
			holder = (Holder) row.getTag();
		}

		Charges charges = data[position];
		
		if(charges.getType()==1){
			suffix = "/" + context.getString(R.string.unit);
		}else{
			suffix = "/" + context.getString(R.string.month);
		}
		
		holder.chargesName.setText(charges.getName());
		holder.chargesPrice.setText(df.format(charges.getDefaultPrice()) + suffix);
		
		return row;
	}

	static class Holder {
		TextView chargesName, chargesPrice;
	}
}
